"""
@ Author        : 马烈勇
@ CreateTime    : malieyong@163.com
@ ModifyTime    : 2021/9/29 上午1:32
@ Version       : Python 3.8
@ FileName      : jwt_auth.py
@ Function      :  
@ Description   : 
"""

import jwt
import datetime

from django.conf import settings


# 需要传入
def create_token(payload, timeout=8):
    salt = settings.SECRET_KEY
    # 构造header
    headers = {
        'typ': 'jwt',
        'alg': 'HS256'
    }
    # 其实就是往payload加入了有效时间
    payload['exp'] = datetime.datetime.utcnow() + datetime.timedelta(hours=timeout)  # 有效时间

    token = jwt.encode(payload=payload, key=salt, algorithm='HS256', headers=headers).decode('utf-8')
    return token
